############################
# ANALYSIS: ETHNIC MATCHING
# Table A4
# 
# Part of replication of:
# Traditional Institutions in Africa, Past and Present
#
# Political Science Research and Methods
#
# By Clara Neupert-Wentz and Carl Müller-Crepon, 2023
#
##########################################

# Load data
plot.df <- readRDS("data/main_data.rds")

## Matched to tradinst
plot.df$matched <- ifelse(!plot.df$group %in% c("", NA, "NA"), 1, 0)

## Split border variable
plot.df$split <- ifelse(duplicated(plot.df$gid) | duplicated(plot.df$gid, fromLast = T), 1, 0)



## Simple model

### Prepare
spec.ls <- list(c("log(pop.1880+1)", "log(poly.area)"),
                c("split"),
                c("v33.num"),
                c("factor(colonizer.all)"))
spec.ls <- c(spec.ls, list(unlist(spec.ls)))            

### Estimate
model.ls <- lapply(spec.ls, function(s){
  felm( make_form(dv = "matched", 
                  expl = s,
                  fe = "0", iv = "0", se = "0"),
        data = plot.df)
})

### Covariate labels
cov.labs <- c("Constant", "Population (1880; log)", "Area (log)", "Split (0/1)",
              "Jurisdictional Hierarchy (v33)", "Belgian colony", "British colony", "French colony", "Portuguese colony")


### Return stargazer
fileConn<-file(file.path(tab.path, paste0("matching_quality", ".tex")))
writeLines(
  stargazer(model.ls,
            title="Covariates of successful link between Murdock and TradGovGroup data",
            dep.var.caption = "Matched Murdock Group (0/1)",dep.var.labels.include = FALSE,
            covariate.labels = cov.labs,
            notes.align = "l",label = "matching_quality",align =T,
            digits = 3, intercept.top = T,intercept.bottom = F,
            omit.stat = c("rsq","res.dev","ser"),column.sep.width = "-5pt",
            type  = "latex"), 
  fileConn)
close(fileConn)




